window.onload = function () {
    window.vue = new Vue({
        el: '#my-div',
        data: {
            list: [],
            id: '',
            name: '',
            pwd: '',
            phone: '',
            role: '',
            index: '',
            roles: [],
        },
        methods: {
            find: function () {
                doGet({
                    url: 'user/user/query',
                    data: {},
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.list = res.data.data;
                            $('#table-main')[0].style.visibility = 'visible';
                        } else {
                            console.log(res);
                        }
                    }
                });
            },

            queryAllRoles: function (callback) {
                doGet({
                    url: 'user/role/query',
                    data: {},
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.roles = res.data.data;
                            callback()
                        } else {
                            console.log(res);
                        }
                    }
                });
            },


            del: function (index) {
                window.parent.showConfirm('重要提示', '您确定要删除此记录吗?', function () {
                    let id = vue.list[index].id;
                    vue.index = index;
                    doPost({
                        url: 'user/user/delete',
                        data: {
                            id: id,
                        },
                        callback: function (res) {
                            if (res.data.code === 1) {
                                vue.list.removeAt(vue.index);
                                vue.list = vue.list.clone();
                                window.parent.showSuccess();
                            } else {
                                window.parent.showInfo(res.data.msg);
                            }
                        }
                    });
                });
            },
            showUpdate: function (i) {
                if (this.roles.length === 0) {
                    this.queryAllRoles(function () {
                        vue.showUpdate(i);
                    });
                    return;
                }
                if (i >= 0) {
                    this.id = this.list[i].id;
                    this.name = this.list[i].name;
                    this.phone = this.list[i].phone;
                    this.role = this.list[i].role;
                    this.index = i;
                }
                $('#update-modal').modal('show');

                setTimeout(function () {
                    if (i >= 0) {
                        $('#role').select2().val(vue.list[i].role_id).change();
                    }
                    else {
                        $('#role').select2();
                    }
                }, 200);
            },

            save: function () {
                let role = $('#role').val();
                doPost({
                    url: 'user/user/update',
                    data: {
                        id: vue.id,
                        name: vue.name,
                        phone: vue.phone,
                        pwd: hex_md5(vue.pwd),
                        role: role,
                    },
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.find();
                            window.parent.showSuccess();
                            $('#update-modal').modal('hide');
                        } else {
                            window.parent.showInfo(res.data.msg);
                        }
                    }
                });
            },

        },
    });
    vue.find();

    $('#inputRole').bind('keypress', function (event) {
        if (event.keyCode === 13) {
            vue.save();
        }
    });

};
